package com.cqhilink.iot.jobstandardmanagement.services.innerservice.impl.teamlog;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.cqhilink.api.common.utils.UniqueUtil;
import com.cqhilink.api.common.utils.date.DateStyle;
import com.cqhilink.api.common.utils.date.DateUtil;
import com.cqhilink.iot.jobstandardmanagement.common.enums.BootstrapUtil;
import com.cqhilink.iot.jobstandardmanagement.dao.mapper.TeamLogMapper;
import com.cqhilink.iot.jobstandardmanagement.facade.model.*;
import com.cqhilink.iot.jobstandardmanagement.facade.service.employee.JsmEmployeeService;
import com.cqhilink.iot.jobstandardmanagement.facade.service.file.JsmFileService;
import com.cqhilink.iot.jobstandardmanagement.facade.service.teaminfo.JsmTeamInfoService;
import com.cqhilink.iot.jobstandardmanagement.facade.service.teaminfo.JsmTeamMembersService;
import com.cqhilink.iot.jobstandardmanagement.facade.service.teamlog.JsmTeamLogService;
import org.apache.commons.collections.map.HashedMap;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by zmling on 2016/12/19.
 * 班组记录信息
 */

public class JsmTeamLogServiceImpl implements JsmTeamLogService {
    @Autowired
    private TeamLogMapper teamLogMapper;
    @Autowired
    private JsmEmployeeService employeeService;
    @Autowired
    private JsmFileService fileService;
    @Autowired
    private JsmTeamInfoService teamInfoService;
    @Autowired
    private JsmTeamMembersService jsmTeamMembersService;


    /**
     * 查詢列表
     *
     * @param bootstrapSearchBean
     * @return
     * @throws Exception
     */
    @Override
    public JSONObject findTeamLog(BootstrapSearchBean bootstrapSearchBean) throws Exception {
        JSONObject jsonObject = new JSONObject();
        bootstrapSearchBean.setLimit(bootstrapSearchBean.getLimit() == null ? 10 : bootstrapSearchBean.getLimit());
        bootstrapSearchBean.setOffset(bootstrapSearchBean.getOffset() == null ? 0 : bootstrapSearchBean.getOffset());
        bootstrapSearchBean.setFilterMap(BootstrapUtil.filterStringToMap(bootstrapSearchBean.getFilter()));
        if (bootstrapSearchBean.getSearch() != null && bootstrapSearchBean.getSearch() != "") {
            bootstrapSearchBean.setSearch(bootstrapSearchBean.getSearch().trim());
        }
        List<TeamLog> list = teamLogMapper.queryList(bootstrapSearchBean);
        jsonObject.put("total", teamLogMapper.queryCount(bootstrapSearchBean));
        jsonObject.put("rows", JSON.toJSON(list));
        return jsonObject;
    }

    /**
     * 新增交接班記錄
     *
     * @param teamLog
     * @param uploadFiles
     * @param deletedFiles
     * @return
     * @throws Exception
     */
    @Override
    @Transactional(propagation = Propagation.REQUIRED)
    public int addTeamLog(TeamLog teamLog, String uploadFiles, String deletedFiles, Employee employee) throws Exception {
        teamLog.setTeamLogId(UniqueUtil.uuid());
        teamLog.setCreatorId(employee.getEmployeeId());
        Date date = new Date();
        teamLog.setCreateDT(DateUtil.DateToString(date, DateStyle.YYYY_MM_DD_HH_MM));
        teamLog.setOperateId(employee.getEmployeeId());
        teamLog.setOperateDT(DateUtil.DateToString(date, DateStyle.YYYY_MM_DD_HH_MM));
        teamLog.setStatus((short) 0);
        teamLog.setConnectDT(DateUtil.DateToString(date, DateStyle.YYYY_MM_DD_HH_MM));
        int a = teamLogMapper.insertSelective(teamLog);
        int i = fileService.addFile(uploadFiles, deletedFiles, teamLog.getTeamLogId());
        //更新交接班组状态 交班状态为2，接班状态为3
        TeamInfo teamInfo = new TeamInfo();
        teamInfo.setTeamId(teamLog.getHandTeamId());
        teamInfo.setStatus((short) 2);
        teamInfoService.teamInfoUpdate(teamInfo, null, null, employee);
        teamInfo.setTeamId(teamLog.getMeetTeamId());
        teamInfo.setStatus((short) 3);
        teamInfoService.teamInfoUpdate(teamInfo, null, null, employee);
        return 0;
    }

    /**
     * 通过Id查询
     *
     * @param teamLogId
     * @return
     * @throws Exception
     */
    @Override
    public TeamLog findToView(String teamLogId) throws Exception {
        return teamLogMapper.selectByPrimaryKey(teamLogId);
    }

    /**
     * @param record
     * @return List<TeamLog>
     * @Author 卢曼成
     * @Description 根据条件查询集合
     * @CreateDateTime 2017/3/29 9:08
     **/
    @Override
    public List<TeamLog> selectByParamList(TeamLog record) {
        return teamLogMapper.selectByParamList(record);
    }

    /**
     * 根据人员信息查询班组信息
     *
     * @param employee
     */
    @Override
    public Map<String, Object> getTeamLogParam(Employee employee) {
        if (employee != null) {
            String employeeId = employee.getEmployeeId();
            Map<String, Object> map = new HashMap<String, Object>();
            TeamInfo teamInfo = teamInfoService.selectByParam(employeeId);
            if (teamInfo != null) {
                map.put("info", teamInfo);
            } else {
                TeamMembers teamMembers = jsmTeamMembersService.selectByEmployeeId(employeeId);
                if (teamMembers != null) {
                    map.put("info", teamMembers);
                }
            }
            return map;
        }
        return null;
    }
}
